package com.tongzhou.tongzhou.quot.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.quot.dao.IPaperDAO;
import com.tongzhou.tongzhou.quot.model.Paper;
import com.tongzhou.tongzhou.quot.model.SearchPaperCondition;

public class PaperAction extends ActionSupport{
	private int id;
	private int paper_type_id;
	private Paper paper;
	private List<Paper> result=null;
	private SearchPaperCondition condition=null;
	
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	private String keng;
	
	private IPaperDAO dao=null;
	/**
	 * 纸质列表界面
	 *@author: 彭定雄
	 *@return 
	 *@throws DBException
	 */
	public String listPaper() throws DBException{
		checkSearchCondition();
		condition = new SearchPaperCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setCurrentPage(currentPage) ;
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType) ;
		condition.setPageSize(pageSize) ;
		if(paper==null){
			condition.setPaper_type_id(paper_type_id);
		}else{
//			condition.setPaper_type_id(paper.getPaper_type_id());
		}
		condition.setKeng(keng);
	    result = dao.listPaper(condition);
		return "listPaper";
	} 
	
	
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "id" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "desc" ;
		}
	}
	
	/**
	 * 新增纸质
	 *@author: 彭定雄
	 *@return
	 *@throws DBException
	 */
	public String toAddPaper() throws DBException{
		return "addPaper";
	}
	
	public String addPaper() throws DBException{
		dao.addPaper(paper);
		listPaper();
		return "listPaper";
	}
	
	public String deletePaper() throws DBException{
//		dao=new UserMgrDAO();
		dao.deletePaper(id);
		listPaper();
		return "listPaper";
	}
	
	public String toEditPaper() throws DBException{
		paper=dao.getPaperById(id);
		return "editPaper";
	}
	
	public String editPaper() throws DBException{
		dao.updatePaper(paper);
		listPaper();
		return "listPaper";
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public int getPaper_type_id() {
		return paper_type_id;
	}


	public void setPaper_type_id(int paperTypeId) {
		paper_type_id = paperTypeId;
	}


	public Paper getPaper() {
		return paper;
	}


	public void setPaper(Paper paper) {
		this.paper = paper;
	}


	public List<Paper> getResult() {
		return result;
	}


	public void setResult(List<Paper> result) {
		this.result = result;
	}


	public SearchPaperCondition getCondition() {
		return condition;
	}


	public void setCondition(SearchPaperCondition condition) {
		this.condition = condition;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	public String getOrderBy() {
		return orderBy;
	}


	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}


	public String getOrderByType() {
		return orderByType;
	}


	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}

	public String getKeng() {
		return keng;
	}


	public void setKeng(String keng) {
		this.keng = keng;
	}


	public IPaperDAO getDao() {
		return dao;
	}


	public void setDao(IPaperDAO dao) {
		this.dao = dao;
	}
}
